let Mcaptcha = require('../../utils/mcaptcha.js');
// pages/personal/personal.js
Page({

  /**
   * 页面的初始数据
   */
  /**
   * 控件当前显示的数据
   * provinces:所有省份
   * citys 选择省对应的所有市,
   * areas选择市对应的所有区
   * areaInfo：点击确定时选择的省市县字符拼接
   * animationAddressMenu：动画
   * addressMenuIsShow：是否可见
   */
  data: {
    region: ['江苏省', '无锡市', '滨湖区'],
    name: '',
    idNumber: '',
    tel: '',
    imgcode: '',
    imgUrls: [
      '../../images/banner.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    menulist: ['女性健康', '老年人健康', '儿童健康', '个性化用药', '遗传/生殖', '肿瘤', '心血管', '内分泌', '消化'],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    userInfo: {},
    num:'',
    identityCardShow:'',
  },

  /**
   * 生命周期函数--监听页面加载
   * 默认地址
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    var num = that.getRanNum();
    var region =  that.data.userInfo.region;
    if (region != null){
      if (region[0] == null) {
        region[0] = '';
      } else if (region[1] == null) {
        region[2] = '';
      } else if (region[2] == null) {
        region[2] = '';
      }
    }
    this.setData({
      num: num,
      region: region
    });
    new Mcaptcha({
      el: 'canvas',
      width: 80,//对图形的宽高进行控制
      height: 30,
      code: num
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userInfo = wx.getStorageSync('trUserInfo');
    var identityCardShow = "";
    if (userInfo.identityCard != null){
      identityCardShow = userInfo.identityCard.replace(/^(.{6})(?:\d+)(.{4})$/, "$1****$2");
    }
    this.setData({
      userInfo: userInfo,
      identityCardShow: identityCardShow
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  name: function(e){
    var that = this;
    this.setData({
      name: e.detail.value
    })
  },

  idNumber: function (e) {
    this.setData({
      idNumber: e.detail.value
    })
  },

  tel: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },

  imgcode: function (e) {
    this.setData({
      imgcode: e.detail.value
    })
  },
  getRanNum: function () {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var pwd = '';
    for (var i = 0; i < 4; i++) {
      if (Math.random() < 48) {
        pwd += chars.charAt(Math.random() * 48 - 1);
      }
    }
    return pwd;
  },
  personSubmit: function (e) {
    console.log("提交验证开始");
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var idcard_patter = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    var that = this;
    that.setData({
      name: e.detail.value.username,
      idNumber: that.data.userInfo.identityCard == null ? e.detail.value.identityCard : that.data.userInfo.identityCard,
      tel: e.detail.value.phoneNumber,
      imgcode: e.detail.value.imgcode
    })
    if (that.data.name.length == 0) {
      wx.showToast({

        title: '姓名不得为空!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    } else if (idcard_patter.test(that.data.idNumber) == false) {
      console.log(idcard_patter.test(that.data.idNumber));
      wx.showToast({

        title: '身份证有误!',

        icon: 'loading',

        duration: 1500

      })
      setTimeout(function () {

        wx.hideToast()

      }, 2000)
    } else if (that.data.tel.length == 0 ) {

      wx.showToast({

        title: '手机号不得为空!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    } else if (that.data.tel.length != 11) {

      wx.showToast({

        title: '请输入11位手机号!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    } else if (!myreg.test(that.data.tel)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    } 
    else if (that.data.imgcode.toUpperCase() != that.data.num.toUpperCase() ) {
      wx.showToast({

        title: '验证码错误!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    } else {
      console.log("提交验证开始2");
      wx.request({

        url: getApp().globalData.host_url + '/wxUser/ajax_wxUserBind',

        header: {

          "Content-Type": "application/json"

        },

        method: "POST",
        dataType:"json",
        data: JSON.stringify({ userId: that.data.userInfo.userId,remark: that.data.name, phoneNumber: that.data.tel, identityCard: that.data.idNumber, region: that.data.region}),

        success: function (res) {

          if (res.data.status == 1) {

            wx.showToast({

              title: res.data.info,

              icon: 'loading',

              duration: 1500

            })

          } else {
            var userInfo = res.data.userInfo;
            wx.setStorageSync('trUserInfo', userInfo);
            wx.showToast({

              title: res.data.info,//这里打印出登录成功

              icon: 'success',

              duration: 1000

            })
            wx.redirectTo({
              url: '../index/index',
            })

          }

        }

      })

    }

  }
})